package base.view.ui

import android.content.DialogInterface
import android.os.Bundle
import androidx.annotation.StyleRes
import android.view.*
import base.contract.OnClickListener
import base.utils.DialogViewHolder
import base.utils.SysUtils
import com.tange.ai.iot.client.gnssa.R

/**
 * @author njmsir
 * Created by njmsir on 2017/9/18.
 */
abstract class BaseDialog : androidx.fragment.app.DialogFragment() {

    companion object {
        val MARGIN = "margin"
        val WIDTH = "width"
        val HEIGHT = "height"
        val DIM = "dim_amount"
        val BOTTOM = "bottom"
        val TOP = "top"
        val CANCEL = "cancel"
        val ANIM = "anim_style"
        val LAYOUT = "layout_id"
        val LISTENER = "listener"
    }

    private var margin: Int = 0
    private var width: Int = 0
    private var height: Int = 0
    private var dimAmount: Float = 0.5f
    private var showBottom: Boolean = false
    private var showTop: Boolean = false
    private var outCancel: Boolean = false
    @StyleRes
    private var animStyle: Int = 0

    protected var layoutId: Int = 0

    var onClickListener: DialogInterface.OnClickListener? = null
    lateinit var dialogView: View
    private var fm: androidx.fragment.app.FragmentManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setStyle(androidx.fragment.app.DialogFragment.STYLE_NO_TITLE, R.style.base_dialog)
        layoutId = dialogLayoutId()

        //恢复保存的数据
        if (savedInstanceState != null) {
            margin = savedInstanceState.getInt(MARGIN)
            width = savedInstanceState.getInt(WIDTH)
            height = savedInstanceState.getInt(HEIGHT)
            dimAmount = savedInstanceState.getFloat(DIM)
            showBottom = savedInstanceState.getBoolean(BOTTOM)
            showTop= savedInstanceState.getBoolean(TOP)
            outCancel = savedInstanceState.getBoolean(CANCEL)
            animStyle = savedInstanceState.getInt(ANIM)
            layoutId = savedInstanceState.getInt(LAYOUT)
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        dialogView = inflater.inflate(layoutId, container, false)
        convertView(DialogViewHolder.create(dialogView), this)
        dialog?.setOnKeyListener { dialog, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                if (this.dialog!!.isShowing) {
                    dialog.dismiss()
                }
                true
            }
            false
        }
        return dialogView
    }

    override fun onStart() {
        super.onStart()
        initParams()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt(MARGIN, margin)
        outState.putInt(WIDTH, width)
        outState.putInt(HEIGHT, height)
        outState.putFloat(DIM, dimAmount)
        outState.putBoolean(BOTTOM, showBottom)
        outState.putBoolean(TOP, showTop)
        outState.putBoolean(CANCEL, outCancel)
        outState.putInt(ANIM, animStyle)
        outState.putInt(LAYOUT, layoutId)
    }

    private fun initParams() {
        val window = dialog?.window
        if (window != null) {
            val layoutParams = window.attributes
            //调节灰色背景透明度[0-1],默认0,5f
            layoutParams.dimAmount = dimAmount
            //是否在底部显示
            if (showBottom) {
                layoutParams.gravity = Gravity.BOTTOM
            }

            if (showTop) {
                layoutParams.y = 100
                layoutParams.gravity = Gravity.TOP
            }

            //设置dialog宽度
            when (width) {
                0 -> layoutParams.width = SysUtils.getScreenWidth(requireContext()) - 2 * SysUtils.dp2px(requireContext(), margin.toFloat())
                -1 -> layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT
                else -> layoutParams.width = width
            }

            //设置dialog高度
            when (height) {
                0 -> layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
                else -> layoutParams.height = height
            }

            //设置dialog进入、退出的动画
            when (animStyle) {
                0 -> {
                    animStyle = R.style.dialog_default_animation
                    window.setWindowAnimations(animStyle)
                }
                -1 -> {
                }
                else -> window.setWindowAnimations(animStyle)
            }
            window.attributes = layoutParams
        }
        isCancelable = outCancel
    }

    abstract fun convertView(holder: DialogViewHolder, dialog: BaseDialog)

    abstract fun dialogLayoutId(): Int

    fun setMargin(margin: Int): BaseDialog {
        this.margin = margin
        return this
    }

    fun setWidth(width: Int): BaseDialog {
        this.width = width
        return this
    }

    fun setHeight(height: Int): BaseDialog {
        this.height = height
        return this
    }

    fun setDimAmount(dimAmount: Float): BaseDialog {
        this.dimAmount = dimAmount
        return this
    }

    fun setShowBottom(showBottom: Boolean): BaseDialog {
        this.showBottom = showBottom
        return this
    }

    fun setShowTop(showTop: Boolean): BaseDialog {
        this.showTop = showTop
        return this
    }

    fun setOutCancel(outCancel: Boolean): BaseDialog {
        this.outCancel = outCancel
        return this
    }

    fun setAnimStyle(@StyleRes animStyle: Int): BaseDialog {
        this.animStyle = animStyle
        return this
    }

    fun setOnCliciListener(onClickListener: OnClickListener): BaseDialog {
        this.onClickListener = onClickListener
        return this
    }

    fun <T : View> getView(viewId: Int): T {
        return dialogView.findViewById(viewId)
    }

    fun showDialog(manager: androidx.fragment.app.FragmentManager, tag: String): BaseDialog {
        try {
            fm = manager
            if (dialog != null) {
                if (isAdded) {
                    manager.beginTransaction().show(this).commitNow()
                } else {
                    manager.beginTransaction().remove(this).commitNow()
                    show(manager, tag)
                }
            } else {
                if (!isAdded) {
                    manager.beginTransaction().remove(this).commit()
                    show(manager, tag)
                } else {
                    manager.beginTransaction().show(this).commitNow()
                }
            }
        } catch (e: Exception) {

        }
        return this
    }

    fun dismissDialog() {
        if (dialog != null && dialog!!.isShowing) {
            dialog!!.dismiss()
        } else {
//            fm?.beginTransaction()?.remove(this)?.commitAllowingStateLoss()
        }
    }
}